package demoMod.moduploader.model;

import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ModUploadItem {
    private static final Gson gson = new Gson();

    private File modContentFolder;

    private File modConfigFile;

    private File modImage;

    public File getModContentFolder() {
        return modContentFolder;
    }

    public void setModContentFolder(File modContentFolder) {
        this.modContentFolder = modContentFolder;
    }

    public File getModConfigFile() {
        return modConfigFile;
    }

    public void setModConfigFile(File modConfigFile) {
        this.modConfigFile = modConfigFile;
    }

    public File getModImage() {
        return modImage;
    }

    public void setModImage(File modImage) {
        this.modImage = modImage;
    }

    public ModInfo generateModInfo() {
        if (modConfigFile != null && modConfigFile.exists()) {
            try (FileReader fileReader = new FileReader(modConfigFile, StandardCharsets.UTF_8)) {
                return gson.fromJson(fileReader, ModInfo.class);
            } catch (IOException ignored) {
            }
        }
        return null;
    }

    public void saveConfigFile(ModInfo config) {
        try (FileWriter fileWriter = new FileWriter(modConfigFile, StandardCharsets.UTF_8)) {
            JsonWriter jsonWriter = gson.newJsonWriter(fileWriter);
            jsonWriter.setIndent("  ");
            gson.toJson(config, ModInfo.class, jsonWriter);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
